//用户登录名--用于修改用户时判断
var checkLoginName = "";
var myPageSize = 15;

$(function() {
	
	var departId = $('#departId').val();
	var curDepartId = $('#curDepartId').val();

	//用户管理菜单Panel初始化
	$('#usersPanel').panel({
		border:false
	});
	
	//用户管理菜单Panel初始化
	$('#usersPanel2').panel({
		border:false
	});

	
	//菜单按钮初始化
	$('#addUserBtn').linkbutton({plain:true,iconCls:'icon-user-add'});
	$('#editUserBtn').linkbutton({plain:true,iconCls:'icon-user-edit'});
	$('#delUserBtn').linkbutton({plain:true,iconCls:'icon-user-del'});
	$('#viewUserBtn').linkbutton({plain:true,iconCls:'icon-view'});
	$('#exportUserBtn').linkbutton({plain:true,iconCls:'icon-excel'});
	$('#closeUserBtn').linkbutton({plain:true,iconCls:'icon-close'});
	$('#USER_CU_WINDOW_SAVE').linkbutton({plain:true,iconCls:'icon-save'});
	$('#USER_CU_WINDOW_CLOSE').linkbutton({plain:true,iconCls:'icon-close'});
	
	
	//添加窗口
	$('#usersWindow').window({
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-user',
		width:615,
		height:315
	});

	//用户管理菜单部门树初始化
	$('#usersDepartTreeGrid').treegrid({
		url:'departAjax!loadCurrUserFullDeparts',
		nowrap : false,
		collapsible:true,
		loadMsg:'数据加载中......',
		idField : 'id',
		treeField : 'text',
		border:false,
		columns : [[
		    {title:'名称',field:'text',width:180},
		    {title:'代码',field:'code',width:100}
		]],
		onClickRow: function(row){
			$('#usersDataGrid').datagrid('options').url = 'userAjax!loadUsersData?curDepartId='+ +row.id+'&pageNo=1&pageSize='+ myPageSize;
			$('#usersDataGrid').datagrid("options").pageNumber = 1;   
			$("#usersDataGrid").datagrid('reload'); 
			$("#usersDataGrid").datagrid('getPager').pagination({pageNumber: 1}); 
		},
		onBeforeLoad:function(row,param){
/*			if (row){
				$(this).treegrid('options').url = 'departAjax!loadChildrenTreeGrid?parentId='+row.id;
			} else {
				$(this).treegrid('options').url = 'departAjax!loadTreeGrid';
			}*/
		}
	});	
	
	
	//用户管理菜单用户列表初始化
	$('#usersDataGrid').datagrid({
		url:'userAjax!loadUsersData?curDepartId='+ curDepartId,
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'desc',
		remoteSort: false,
		pagination:true,
		idField:'id',	
		columns:[[
		           {field:'id',title:'ID',width:50},
		           {field:'loginName',title:'用户名',width:100},
		           {field:'name',title:'姓名',width:100},
		           {field:'sn',title:'编号',width:100},
		           {field:'sex',title:'性别',width:60},
		           {field:'departName',title:'部门',width:100},
		           {field:'roleNames',title:'角色',width:300},
		           {field:'status',title:'状态',width:60},
		           {field:'locked',title:'锁定',width:60}
				]]
	});
	
	//用户管理菜单用户列表分页初始化
	var pager = $('#usersDataGrid').datagrid('getPager');
	$(pager).pagination({
		 pageSize: myPageSize,//每页显示的记录条数，默认为10 
	     pageList: [15, 30, 50, 100],//可以设置每页记录条数的列表 
	     beforePageText: '第',//页数文本框前显示的汉字 
	     afterPageText: '页    共 {pages} 页', 
	     displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',  
		 onSelectPage:function(pageNumber, pageSize){
			//获取部门树 选中节点，未选中则为0
			var selectDepartId = 0;
			var node = $('#usersDepartTreeGrid').treegrid('getSelected');
			if(node != null){
				selectDepartId = node.id;
			}
			$('#usersDataGrid').datagrid('options').url = 'userAjax!loadUsersData?curDepartId='+ +selectDepartId+'&pageNo='+ pageNumber +'&pageSize='+pageSize;
			$("#usersDataGrid").datagrid('reload', pageNumber); 
		}
	});

	
	//初始化用户性别
	DICT_INIT(GETID('usersSex'), 'Sex', '', '1');
	
	//生日日期框
	$('#usersBirthday').datebox({
		width:205
	});  
	
	//参加工作时间
	$('#usersJobDate').datebox({
		width:205
	});  
	
	
	//初始化用户职位状态
	DICT_INIT(GETID('usersStatus'), 'PostStatus', '', '1');
	
	//初始化用户状态
	DICT_INIT(GETID('usersLocked'), 'LockStatus', '', '1');
	
	
	//部门下拉树菜单
	$('#usersDepart').combotree({
		width:204,
		url:'departAjax!loadCurrUserFullDeparts'
		//url:'departAjax!loadFullTree?parentId='+departId
	});

	
	//角色下拉菜单
	$('#usersRoles').combobox({
		width:204,
		url:'roleAjax!loadRoleDataList',
		valueField:'id',
		textField:'name',
		multiple:true
	});
	
	//var treeRoot = $('#usersDepartTreeGrid').treegrid('getRoot');
	
});


//打开窗口
function openUsersWindow(type) {
	clearTable();
	if(type == 'winClose'){
		currTabClose();
	}else if(type == 'del'){
		delUser();
		return;
	}else if(type == 'add'){
		$('#usersMethodName').val('add');
		$('#usersWindow').window({title:'增加'});
	}else if(type == 'edit'){
		var selectedRow = $('#usersDataGrid').datagrid('getSelected');
		if(selectedRow==null){
			$.messager.alert('提示','请先选择一条记录!');
			return;
		}else{
			$('#usersMethodName').val('editAll');
			$('#usersWindow').window({title:'编辑'});
			//$('#submitUsersButton').val('编辑');
			//控件赋值
			$('#userLoginName').val(selectedRow.loginName);
			$('#userName').val(selectedRow.name);
			$('#pwd').val(selectedRow.password);
			$('#usersDepart').combotree('setValue',selectedRow.departId);
			var roleIds = selectedRow.strRoleIds.split(",");
			$('#usersRoles').combobox('setValues', roleIds);
			$('#userSn').val(selectedRow.sn);
			$('#usersSex').combobox('setValue', selectedRow.sex);
			$('#userPhone').val(selectedRow.phone);
			$('#usersBirthday').datebox('setText', selectedRow.birthday);
			$('#usersJobDate').datebox('setText', selectedRow.jobDate);
			$('#usersStatus').combobox('setValue', selectedRow.status);
			$('#usersLocked').combobox('setValue', selectedRow.locked);
			checkLoginName = selectedRow.loginName;
		}
	}else{
		$.messager.alert('提示','非法操作!',"error");
		return;
	}
	$('#usersWindow').window('open');	
	$('#userLoginName').focus();
}

function submitUser(){
	var usersMethodName = $('#usersMethodName').val();
	var userLoginName = $('#userLoginName').val();
	var userName = $('#userName').val();
	var pwd = $('#pwd').val();
	var userPwd = $('#userPwd').val();
	var userDepart = $('#usersDepart').combotree('getValue');
	var userRoles = $('#usersRoles').combobox('getValues');
	var strUserRoles = "";
	var userSn = $('#userSn').val();
	var userSex = $('#usersSex').combobox('getText');
	var userPhone = $('#userPhone').val();
	var userBirthday = $('#usersBirthday').datebox('getText');
	var userJobDate = $('#usersJobDate').datebox('getText');
	var userStatus = $('#usersStatus').combobox('getText');
	var userLocked = $('#usersLocked').combobox('getText');

	//验证用户信息
	if(userLoginName==''){
		$.messager.alert('提示','用户名不能为空!');
		return;
	}
	
	//如果隐藏密码为空
	if(pwd == ''){
		//判断密码框为空  则提示错误
		if(userPwd==''){
			$.messager.alert('提示','密码不能为空!');
			return;
		}
	}
	
	if(userName==''){
		$.messager.alert('提示','姓名不能为空!');
		return;
	}

	if(userDepart==''){
		$.messager.alert('提示','未选用户的所在部门!');
		return;
	}
	if(userRoles==''){
		$.messager.alert('提示','未选用户的角色!');
		return;
	}
	
	//循环角色ID
	for(i=0;i<userRoles.length;i++){
		if(i!=0){
			strUserRoles += ","; 
		}
		strUserRoles += userRoles[i];
	}
	//如果角色字符串第一位是 ，则过滤掉
	if(strUserRoles.charAt(0) == ","){
		strUserRoles = strUserRoles.substring(1);
	}
	
	//判断操作类型
	switch(usersMethodName){
		//添加用户
		case 'add':	
			//验证用户名是否重复
			$.post('userAjax!checkLoginName', {loginName:userLoginName},
				function(data){
					if(data.result=='false'){
						$.messager.alert('提示','用户名已存在!');
						return;
					}else{
						$.post('userAjax!add', {loginName:userLoginName, name:userName, password:userPwd, departId:userDepart, roles:strUserRoles,
							sn:userSn, sex:userSex, phone:userPhone, jobDate:userJobDate, birthday:userBirthday, status:userStatus, locked:userLocked},
								function(data){
									$('#usersDataGrid').datagrid('reload');
							    }, 'json');
					}
			}, 'json');
			//用户添加
			
			break;
		//修改用户
		case 'editAll':
			var selectedRow = $('#usersDataGrid').datagrid('getSelected');
			//alert(strUserRoles);
			//用户修改
			$.post('userAjax!edit', {id:selectedRow.id, loginName:userLoginName, name:userName, password:userPwd, departId:userDepart, roles:strUserRoles,
				sn: userSn, sex: userSex, phone: userPhone, jobDate: userJobDate, birthday: userBirthday, status: userStatus, locked: userLocked},
				function(data){
					$("#usersDataGrid").datagrid('reload'); 
				}, 'json');
			break;
		}
		$('#usersWindow').window('close');	
} 

function delUser(){
	var selectedRow = $('#usersDataGrid').datagrid('getSelected');
	var selectedIndex = $('#usersDataGrid').datagrid('getRowIndex', selectedRow);
	if(selectedRow == null){
		$.messager.alert('提示','请先选择一个用户!');
		return;
	}else{
		$.messager.confirm('提示', '确认要删除此用户吗?', function(r){
			if (r){
				$.post('userAjax!delete', {id:selectedRow.id},
			        	function(data){
							$('#usersDataGrid').datagrid('deleteRow', selectedIndex);
			        	}
			        	, 'json');
			}

		});
	}
}

function closeWindow(){
	$('#usersWindow').window('close');
}

function clearTable(){
	$('#userLoginName').val('');
	$('#userName').val('');
	$('#pwd').val('');
	$('#userPwd').val('');
	$('#usersDepart').combotree('clear');
	$('#usersRoles').combobox('clear');
	$('#userSn').val('');
	//$('#usersSex').combobox('clear');
	$('#userPhone').val('');
	$('#usersBirthday').datebox('clear');
	$('#usersJobDate').datebox('clear');
	//$('#usersStatus').combobox('clear');
	//$('#usersLocked').combobox('clear');
	//$('#usersLocked').combobox('clear');
}

